// Created by Roman Novitsky on 25.05.2009


#region Imported Namespaces


using System;
using System.Diagnostics;

using WtfManager.EditingHistory;


#endregion


namespace WtfManager.FileManager
{
  public class MoveChange: Change
  {
    #region Fields


    private readonly FileSystemItem _item;
    private readonly FileSystemFolder _target;
    private readonly FileSystemFolder _previousParent;


    #endregion


    #region Constructors/Finalizers


    public MoveChange(FileSystemFolder dropTarget, FileSystemItem droppedItem)
    {
      _target = dropTarget;
      _item = droppedItem;
      _previousParent = (FileSystemFolder)droppedItem.Parent;
    }


    #endregion


    #region Properties/Indexers


    public override string Details
    {
      get
      {
        return string.Format("Moved '{0}' from {1} to '{2}'.", _item.Name, _previousParent.Name, _target.Name);
      }
    }

    public override string Name
    {
      get
      {
        return "Move";
      }
    }


    #endregion


    #region Methods


    protected override void Execute()
    {
      MoveTo(_target);
    }


    protected override void Revert()
    {
      MoveTo(_previousParent);
    }


    private void MoveTo(FileSystemFolder folder)
    {
      try
      {
        _item.Move(folder);
      }
      catch (Exception e)
      {
        Debug.Fail(e.Message);

        return;
      }
    }


    #endregion
  }
}